<%@ page import="java.util.ArrayList" %>
<%@ page import="java.util.HashMap" %>
<%@ page import="com.du.bean.Person" %>
<%@ page import="com.du.bean.Student" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2020/2/4
  Time: 14:04
  To change this template use File | Settings | File Templates.
--%>
<html>
<head>
    <title>Title</title>
</head>
<body>
<%--设置域对象的域属性，直接使用c:set的方法--%>
<%--以下两种方法，等效--%>
<%--但比较奇怪的是，value只能设置为String，感觉没什么用--%>
<c:set scope="page" var="key" value="value"/>
<%
    pageContext.setAttribute("key1", "value1");
%>
${pageScope.key}<br>
${pageScope.key1}<br>
<hr>

<%--if判断--%>
<%--判断条件放入test里，需要里面需要放入boolean，可以通过Java的表达式或EL表达式控制--%>
<c:if test="true">
    <h1>true</h1>
</c:if>
<c:if test="<%=1==1%>">
    <h1>true</h1>
</c:if>
<c:if test="${1==1}">
    <h1>true</h1>
</c:if>
<hr>

<%
    pageContext.setAttribute("age", 50);
%>
<%--多路判断--%>
<%--类似于switch-case--%>
<c:choose>
    <c:when test="${age<10}">
        <span>age<10</span>
    </c:when>
    <c:when test="${age<20}">
        <span>age<20</span>
    </c:when>
    <c:when test="${age<30}">
        <span>age<30</span>
    </c:when>
    <c:when test="${age<40}">
        <span>age<40</span>
    </c:when>
    <c:otherwise>
        <span>age>=40</span>
    </c:otherwise>
</c:choose>
<hr>
<%--for-each--%>
<%--遍历数字--%>
<c:forEach begin="0" end="20" var="i">
    <span>${i}</span>
</c:forEach>
<br>
<%--遍历数组--%>
<%
    String[] strings = new String[]{"A", "B", "C", "D"};
    request.setAttribute("strings", strings);
%>
<c:forEach items="${requestScope.strings}" var="item">
    <span>${item}</span>
</c:forEach>
<br>
<%--遍历List--%>
<%
    ArrayList<Integer> arrayList = new ArrayList<>();
    arrayList.add(1);
    arrayList.add(2);
    arrayList.add(3);
    arrayList.add(4);
    request.setAttribute("arrayList", arrayList);
%>
<c:forEach items="${requestScope.arrayList}" var="item">
    <span>${item}</span>
</c:forEach>
<br>
<%--遍历Map--%>
<%
    HashMap<String, Integer> hashMap = new HashMap<>();
    hashMap.put("one", 1);
    hashMap.put("two", 2);
    hashMap.put("three", 3);
    hashMap.put("four", 4);
    request.setAttribute("hashMap", hashMap);
%>
<c:forEach items="${requestScope.hashMap}" var="entry">
    <span>
        ${entry.key} = ${entry.value}
    </span>
</c:forEach>
<br>
<%--遍历bean类构成的List--%>
<%
    ArrayList<Student> students = new ArrayList<>();
    for (int i = 0; i < 10; i++) {
        students.add(new Student("name" + i, i));
    }
    request.setAttribute("students",students);
%>

<c:forEach items="${students}" var="item">
    <span>${item.name}</span>
    <span>${item.age}</span>
    <br>
</c:forEach>
</body>
</html>
